<template>
	<view class="preview-article">
		<!-- 标题 -->
		<view class="title">
			{{title}}
		</view>
		<!-- 头像昵称 -->
		<view class="user-info">
			<view class="head-img">
				<image :src="userInfo.avatarUrl" mode="aspectFill"></image>
			</view>
			<view class="nic">{{userInfo.nickName}}</view>
		</view>
		<!-- 正文 -->
		<view class="content">
			<view
			v-for="con in content"
			:key="con.id">
				<view v-if="con.content" class="para">
					{{con.content}}
				</view>
				<view v-else :style="{'position':'relative'}" class="para">
					<image :src="con.src" mode="aspectFill" class="content-img"></image>
					<view class="img-des">{{con.des}}</view>
				</view>
			</view>
		</view>
		
		<!-- 保存和发布 -->
		<button class="save-article" @click="save">保存并发布</button>
	</view>
</template>

<script>
	import {mapState} from 'vuex'
	import common from '../../util/common.js'
	
	export default {
		data() {
			return {
				title:''
			}
		},
		methods: {
			save(){
				let _this = this;
				uniCloud.callFunction({
					name:'article',
					data:{
						type:'add',
						articleInfo:{
							title:_this.title,
							content:_this.content,
							user_id:_this.user_id,
							read_num:0,
							paraise_num:0
						}
					}
				}).then(res => {
					console.log(res);
					uni.showModal({
						content:'文章发布成功！',
						showCancel:false
					})
				}).catch(err => {
					uni.showModal({
						content:'文章发布失败！' + err,
						showCancel:false
					})
					
				})
			}
		},
		onLoad: function (option) { //option为object类型，会序列化上个页面传递的参数
			this.title = option.title; //接收传递的标题
		},
		computed:{
			...mapState(['editContent','userInfo','user_id']),
			content:function(){
				let content = [];
				for(let c of this.editContent){
					if(c.type === 'txt' && c.content.length === 0){
						continue;
					}
					content.push(c)
				}
				return content;
			}
		}
	}
</script>

<style>
	.preview-article {
		padding: 20rpx;
		background-color: #fff;
	}
	.title {
		font-size: 50rpx;
		margin-bottom: 30rpx;
	}
	.user-info {
		display: flex;
		margin-bottom: 30rpx;
		line-height: 50rpx;
	}
	.head-img {
		width: 50rpx;
		height: 50rpx;
		margin-right: 20rpx;
	}
	.head-img image {
		width: 100%;
		height: 100%;
	}
	.para {
		/* text-indent: 2em; */
		padding: 20rpx 0;
		margin-bottom: 60rpx;
	}
	.content-img {
		width: 100%;
	}
	.img-des {
		position: absolute;
		bottom: -60rpx;
		left: 50%;
		transform: translateX(-50%);
		padding:15rpx 25rpx;
		line-height: 50rpx;
		border-bottom:1px solid #aaa;
		color: #aaa;
		font-size: 28rpx;
	}
	.save-article {
		position: fixed;
		bottom: 0;
		left: 0;
		width: 100%;
		background-color: #ff6811;
		color: #fff;
		border-radius: 0;
	}
</style>
